package com.sz.app.oss.api.common.customer;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.customer.service.CustomerCompanyService;
import com.sz.biz.common.customer.dto.CusCompanyDto;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xutao on 17/4/11.
 */
@RestController
@RequestMapping("${api}/customer/company")
@Api(description = " ", tags = "037-003、客户信息管理-公司")
public class CusCompanyController extends BaseController {

    @Autowired
    CustomerCompanyService customerCompanyService;

    /**
     * 新建一个客户归属公司
     *
     * @throws Exception ex
     */
//    @ApiOperation(value = "新建公司", notes = "-<br>权限：" + Permissions.CUSTOMER_MGT)
//    @RequestMapping(value = "", method = {RequestMethod.POST})
//    @RequiresPermissions(value = {Permissions.CUSTOMER_MGT})
//    public ResultDto saveCompany(@RequestBody @Validated CusCompanyDto cusCompanyDto) throws Exception {
//
//        int companyId = customerCompanyService.save(cusCompanyDto);
//
//        ResultDto dto = getSaveSuccessDto(MessagesCodes.MODULE_CUSTOMER);
//
//        dto.setData(companyId);
//
//        return dto;
//    }

    /**
     * 新建一个客户归属公司
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "凭借主账号Id查询公司信息", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "/{customerid}", method = {RequestMethod.GET})
    public QResultDto findCompanyByCustomerId(@PathVariable int customerid) throws Exception {

        CusCompanyDto cusCompanyDto = customerCompanyService.findCompanyByCustomerId(customerid);

        QResultDto qResultDto = new QResultDto();

        qResultDto.setData(cusCompanyDto);

        return qResultDto;
    }

    /**
     * 修改客户归属公司
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "更新公司", notes = "-<br>权限：" +
            Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.CUSTOMER_LOGISTICS_MGT,Permissions.CUSTOMER_TRADE_MGT},
            logical = Logical.OR)
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public ResultDto updateCompany(@RequestBody CusCompanyDto cusCompanyDto) throws Exception {

        int companyId = customerCompanyService.updateCompanyById(cusCompanyDto);
        CusCompanyDto cusCompany = customerCompanyService.findCompanyByCustomerId(cusCompanyDto.getRefCustomerId());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("creator", cusCompany.getCreator());
        resultMap.put("createTime", cusCompany.getCreateTime());
        resultMap.put("id", companyId);
        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_CUSTOMER_COMPANY);
        dto.setData(resultMap);
        return dto;
    }
}
